import { Engine } from "../Engine";
import { ComponentType } from "../component/index";
import { any } from "../core/logic";
import { EventType } from "../event/index";
import { RectangleGeometry } from "./RectangleGeometry";

class Grid extends RectangleGeometry{
    constructor(opts){
        super(opts);
        this.type = "Grid";
        this.pixel=any(opts.pixel,512);//多少个像素一组
        this.on(EventType.ComponentEvent,event=>{
            if(event.componentType === ComponentType.Camera){//相机改变
                this.buildGrid();
            }
        });
    }
    buildGrid(){
        const camera=Engine.instance.scene.getCamera();
        const per=this.width*camera.size[1]/camera.distance;
        this.repeatSeg=Math.pow(2,Math.ceil(Math.log2(per/this.pixel)));
    }
    set pixel(v){
        this._pixel=v;
        this.buildGrid();
        return this;
    }
    get pixel(){
        return this._pixel;
    }
}
export {Grid}